<script setup lang="ts">
import {User, Lock} from "@element-plus/icons-vue";
import {reactive, ref} from "vue";
import useUserStore from "@/stores/modules/user.ts";
import {useRoute, useRouter} from "vue-router";
// 消息提示框
import {ElNotification} from "element-plus";
import {getTime} from "@/utils/time.ts";

const userStore = useUserStore()
const router = useRouter()
const route = useRoute()
const loginForm = reactive({
  username: 'admin',
  password: 'atguigu123'
})
// 定义变量，控制按钮加载
const loading = ref(false)
const loginForms = ref() // 表单域实例对象
const login = async () => {
  // 表单校验如果不成功则不会向下执行
  await loginForms.value.validate()
  loading.value = true
  const redirect:any = route.query.redirect
  try {
    await userStore.userLogin(loginForm)
    await router.push({
        path:redirect || '/'
    })
    ElNotification({
      type: 'success',
      message: '欢迎回来',
      title: `Hi，${getTime()}好`
    })
    loading.value = false
  } catch (error) {
    loading.value = false
    ElNotification({
      type: 'error',
      message: (error as Error).message // 断言
    })
  }
}

const rules = {
  // trigger：触发表单校验的时机
  username: [
    {
      required: true,
      message: '用户名不能为空',
      trigger: "blur" // 失去焦点
      /*
      * 自定义校验规则：validator
      * validator(rule, value, callback)
      * value为表单获取的值
      * 如果符合规则，callback放行
      * 不符合规则，callback注入错误提示
      * */
    },
    {
      required: true,
      min: 5,
      max: 10,
      message: '用户名长度5-10位',
      trigger: 'change' // 改变时
    }],
  password: [
    {
      required:true,
      min:6,
      max:15,
      message:'密码长度6-15位',
      trigger:'change'
    }]
}

</script>

<template>
  <div class="login">
    <el-row>
      <el-col :span="12" :xs="0"></el-col>
      <el-col :span="12" :xs="24">
        <el-form class="login-form" :model="loginForm" :rules="rules" ref="loginForms">
          <h1>Hello</h1>
          <h2>欢迎来到BS</h2>
          <el-form-item prop="username">
            <el-input v-model="loginForm.username" :prefix-icon="User"></el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input v-model="loginForm.password" :show-password="true" type="password" :prefix-icon="Lock"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button :loading="loading" @click="login()" class="login-btn" type="primary" size="default">登录
            </el-button>
          </el-form-item>
        </el-form>
      </el-col>
    </el-row>
  </div>
</template>

<style scoped lang="scss">
.login {
  width: 100%;
  height: 100vh;
  background: url("../../assets/images/login.jpg") no-repeat;
  background-size: cover;

  .login-form {
    position: relative;
    width: 80%;
    //视口的百分之三十
    top: 30vh;
    background-color: #000000;
    padding: 40px;
    opacity: 0.7;

    h1 {
      color: white;
      font-size: 40px;
      opacity: 1 !important;
    }

    h2 {
      color: white;
      font-size: 20px;
      margin: 20px 0;
    }

    .login-btn {
      width: 100%;
    }
  }
}
</style>